package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.utils.DateUtils;
import com.itheima.mapper.CheckGroupItemMapper;
import com.itheima.mapper.CheckitemMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.CheckgroupItem;
import com.itheima.pojo.po.CheckitemPo;
import com.itheima.service.CheckitemService;
import io.netty.handler.codec.MessageAggregationException;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CheckitemServiceImpl implements CheckitemService {
    /**
     * 检查项添加功能
     * @param checkitem
     */
    @Autowired
    private CheckitemMapper checkitemMapper;
    @Autowired
    private CheckGroupItemMapper checkGroupItemMapper;
    public void add(CheckitemPo checkitem) {
        checkitemMapper.addCheck(checkitem);
    }

    /**
     * 检查项修改
     * @param checkitem
     */
    @Override
    public void editCheckItem(CheckitemPo checkitem) {
        checkitemMapper.edit(checkitem);

    }

    /**
     * 分页查询检查项
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());//自定义分页查询

        //数据库的正常查询结果
        Page<CheckItem> list=checkitemMapper.pageQueryCheck(queryPageBean);//返回结果应该是Checkitem集合


        return new PageResult(list.getTotal(),list.getResult());
//
    }

    /**
     * 判断：检查组存在的话，则不能删除
     * @param id
     */
    @Override
    public void deleteByID(Integer id) {
        List<CheckgroupItem> list=checkGroupItemMapper.getByCheckItemId(id);
        if (list!=null || list.size()>0){

            throw new RuntimeException(MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        checkitemMapper.deleteById(id);
    }

    @Override
    public List<CheckItem> findAllCheckItem() {
        List<CheckItem> list=checkitemMapper.findAllCheck();
        return list;
    }

    @Override
    public CheckItem findById(Integer id) {

        return checkitemMapper.findById(id);
    }

    /**
     * 根据groupID查询对应的检查项集合
     * @param groupId
     * @return
     */
    public List<Integer> getGroupId(Integer groupId) {
        //先查询关系表的所有检查项，封装集合后，再查询检查项表
        List<Integer> checkgroupItemList=checkGroupItemMapper.getByGroupId(groupId);
//        if(checkgroupItemList==null || checkgroupItemList.size()==0){
//            return null;//实际上并不需要这一步判断
//        }
        List<Integer> list=new ArrayList<>();
        for (Integer integer : checkgroupItemList) {
            CheckItem id=checkitemMapper.findById(integer);
            list.add(id.getId());
        }
//        List<Integer> itemList=checkitemMapper.getByGroupListId(checkgroupItemList);
//        return checkitemMapper.getByGroupListId(checkgroupItemList);
        return list;
    }
}
